import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/ai/ai_info_entity.dart';
import 'package:sports_new/entity/ai/ai_rate_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/server/api/ai_api.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:sports_new/view/pick/ai/ai_type.dart';
import 'package:sports_new/widgets/max_bottom_sheet.dart';

import '../../../widgets/base/qkd_text.dart';

class AiInfoLabel extends StatelessWidget {
  const AiInfoLabel({super.key, required this.type, this.data, this.rate});

  final AiType? type;
  final List<AiInfoEntity>? data;
  final List<AiRateEntity>? rate;

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: BackdropFilter(
        filter: ImageFilter.blur(
          sigmaX: 20,sigmaY: 20
        ),
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 12,vertical: 8),
          decoration: BoxDecoration(
            color: Colours.white.withOpacity(0.1),
            borderRadius: BorderRadius.circular(8),
          ),
          child: Column(
            children: data?.map(
              (e) => item(e)
            ).toList().insertEvery(Container(height: 6)) ?? []
          ),
        ),
      ),
    );
  }

  Widget item(AiInfoEntity entity){
    return SizedBox(
      height: 17,
      child: DefaultTextStyle(
        style: const TextStyle(fontSize: 12,color: Color(0xFFF9D9D5)),
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: (){
            if(entity.path == '/ai/league'){
              if(rate.hasValue) Get.bottomSheet(rateBottomSheet(),isScrollControlled: true);
            }else if(entity.path.hasValue){
              Utils.doJump(entity.path ?? "");
            }
          },
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              QkdText("${entity.name ?? ""}：",needCenter: true,color: Colours.white),
              if(entity.path == '/ai/league')...[
                ...entity.list?.map<Widget>((e) => Container(
                  height: 17,
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(horizontal: 6),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4),
                    color: Colours.black.withOpacity(0.15),
                    border: Border.all(width: 0.5,color: Colours.white.withOpacity(0.5),strokeAlign: BorderSide.strokeAlignOutside)
                  ),
                  child: QkdText(e,needCenter: true,),
                )).toList().insertEvery(Container(width: 4)) ?? [],
              ]else...[
                ...entity.list?.map<Widget>(
                  (e) => QkdText(e,needCenter: true,)
                ).toList().insertEvery(Container(width: 4)) ?? [],
              ],
              const Spacer(),
              if(entity.path.hasValue)
              Image.asset(
                  width: 12,color: Colours.white,
                  Utils.getImgPath('arrow_right_36.png'))
            ],
          ),
        ),
      ),
    );
  }

  Widget rateBottomSheet(){
    List<String> title = ["联赛","战绩","命中率"];
    return MaxBottomSheet(
      enableDrag: false,
      backgroundColor: Colors.white,
      constraints: BoxConstraints(maxHeight: Get.height*0.6),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(10), topRight: Radius.circular(10))),
      onClosing: () {},
      builder: (context){
      return DefaultTextStyle(
        style: const TextStyle(color: Colours.grey66),
        child: Column(
          children: [
            Container(height: 17),
            QkdText("擅长联赛",fontSize: 16),
            Container(height: 17),
            SizedBox(
              height: 40,
              child: Row(
                children: title.map(
                  (e) => Expanded(
                    child: QkdText(e).center
                  )
                ).toList(),
              ),
            ),
            const Divider(color: Colours.greyEE,height: 0.5).paddingSymmetric(horizontal: 16),
            Expanded(
              child: Stack(
                alignment: Alignment.topCenter,
                children: [
                  SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        ...rate?.map<Widget>((e) => SizedBox(
                          height: 40,
                          child: Row(
                            children: [
                              Expanded(child: QkdText(e.name ?? "").center),
                              Expanded(child: QkdText(e.mn ?? "").center),
                              Expanded(child: QkdText(e.hit ?? "").center)
                            ]
                          ),
                        )).toList().insertWith(rate?.length ?? 0, [
                          Container(height: 30)
                        ]).insertEvery(const Divider(color: Colours.greyEE,height: 0.5).paddingSymmetric(horizontal: 16)) ?? []
                      ],
                    ),
                  ),
                  Positioned(
                    bottom: 0,left: 0,right: 0,
                    child: Container(
                      height: 40,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [Colours.white.withOpacity(0),Colours.white],
                          begin: Alignment.topCenter,end: Alignment.bottomCenter,
                          stops: const [0,0.7]
                        )
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      );
    });
  }
}
